package com.tucc.hello.leetcode.no1_400.no1_200.no41_60.no49;

import java.util.*;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        System.out.println(solution.groupAnagrams(strs));
    }

    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> result = new ArrayList<>();
        Map<String, List<String>> map = new HashMap<>();
        Arrays.stream(strs).forEach(str -> {
            char[] arr = str.toCharArray();
            Arrays.sort(arr);
            String newString = new String(arr);
            List<String> list = map.get(newString);
            if (list == null) {
                list = new ArrayList<>();
                map.put(newString, list);
            }
            list.add(str);
        });
        map.values().forEach(value -> {
            result.add(value);
        });
        return result;
    }
}
